package com.ucomm.talent.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ucomm.talent.AppConfig;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.activity.detail.JobOffersDetails;
import com.ucomm.talent.adapter.JobOffersListAdapter;
import com.ucomm.talent.common.DebugUtil;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.model.JobOfferInfo;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.MainHeaderBar;

import me.maxwin.view.IXListViewLoadMore;
import me.maxwin.view.IXListViewRefreshListener;
import me.maxwin.view.XListView;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
/**
 * 招聘信息界面
 * @author Administrator
 *
 */
public class JobOffers extends BaseActivity implements  IHeaderBarClickListener{
	
	private XListView listView;
	private MainHeaderBar mainHeaderBar;
	private SearchManager searchManager;
	private CustomProgressDialog customProgressDialog;
	private JobOffersListAdapter adapter;
	private ArrayList<JobOfferInfo> data;
	private String url=null;
	private boolean allowProgressShow = true;
	Handler mHandler = new Handler(){
	        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            DebugUtil.Log(JobOffers.this,"---------- " + customProgressDialog.toString());
            switch (what){
                case AppConstants.MsgStatus.MSG_ERROR:
                    if (customProgressDialog != null && customProgressDialog.isShowing())
                        customProgressDialog.dismiss();
                    ((AppException)msg.obj).makeToast(JobOffers.this);
                    break;
                case AppConstants.MsgStatus.MSG_SUCCUSS:
                    if (customProgressDialog != null && customProgressDialog.isShowing())
                        customProgressDialog.dismiss();
                    //放置数据
                    data.addAll((ArrayList<JobOfferInfo>) msg.obj);
                    if(adapter == null){
                    	adapter = new JobOffersListAdapter(JobOffers.this, data);
                    	listView.setAdapter(adapter);
                    }else{
                    	adapter.notifyDataSetChanged();
                    }
                    listView.setOnItemClickListener(new ListItemListener());
                    //上拉加载更多
                    listView.setPullLoadEnable(new IXListViewLoadMore() {
                        @Override
                        public void onLoadMore() {
                        	allowProgressShow = false;
                        	if(AppConfig.CURR_PAGE !=0){
                        		int i = AppConfig.CURR_PAGE;
                        		if(i<AppConfig.PAGE_COUNT){
                        			AppConfig.CURR_PAGE += 1;
                        			String pageUrl = url+"&page="+AppConfig.CURR_PAGE;
                                	searchManager.searchDataFromNetwrok(pageUrl);
                        		}else{
                        			listView.disablePullLoad();
                        		}
                        	}
                        }
                    });
                    //下拉刷新
                    listView.setPullRefreshEnable(new IXListViewRefreshListener() {
                        @Override
                        public void onRefresh() {
                        	allowProgressShow = false;
                        	searchManager.searchDataFromNetwrok(url);
                        	listView.stopRefresh(AppConfig.formatter.format(new Date(System.currentTimeMillis())));
                        }
                        
                    });
                    break;
                case AppConstants.MsgStatus.MSG_LOADING:
                    if (customProgressDialog != null && !customProgressDialog.isShowing() && allowProgressShow)
                        customProgressDialog.show();
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
	    };
	    
	    
	    
	    public void onCreate(Bundle savedInstanceState) {
		    super.onCreate(savedInstanceState);
		    setContentView(R.layout.job_offers_layout);
		    initView();
		    
		    searchManager = new SearchManager(this, mHandler);
		    Intent i = getIntent();
		    String otherUrl = i.getStringExtra("url");
		    if (otherUrl!=null){
		    	url = otherUrl;
//		    	searchManager.searchDataFromNetwrok(url);
//		    }else if(url!=null&&url.contains(AppConstants.JOB_SEARCH)){
		    }else{
		    	url = AppConstants.JOB_OFFERS;
		    }
		    searchManager.searchDataFromNetwrok(url);
	    }
	    private void initView(){
	        mainHeaderBar = (MainHeaderBar) findViewById(R.id.main_header_bar);
	        mainHeaderBar.setBarClickListener(this);
	        customProgressDialog = new CustomProgressDialog(this);
	        listView = (XListView) findViewById(R.id.custom_list);
	        listView.setmHeaderViewBackground(R.color.custom_list_item_bg_color);
	        listView.setmFooterViewBackground(R.color.custom_list_item_bg_color);
	        data = new ArrayList<JobOfferInfo>();
	        
		    }
	//选中项监听
	class ListItemListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			ArrayList<JobOfferInfo> mData = adapter.getmData();
			JobOfferInfo info = mData.get(arg2-1);
			Intent i = new Intent();
			i.putExtra("info", info);
			i.setClass(JobOffers.this, JobOffersDetails.class);
			startActivity(i);
		}
		
	}
	@Override
	public void onBackButtOnClick(View view) {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	public void onExitButtonOnClick(View view) {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		this.allowProgressShow = true;
		
	}

}
